<?php

namespace app\api\controller;

use addons\epay\library\Service;
use app\admin\model\course\ClassesConfig;
use app\admin\model\course\Order;
use app\admin\model\course\OrderGoods;
use app\admin\model\course\OrderSign;
use app\admin\model\course\Transfer;
use app\admin\model\course\UserCoupon;
use app\admin\model\Third;
use app\admin\model\user\Student;
use app\common\controller\Api;
use app\common\model\order\PayLog;
use fast\Date;
use think\Db;
use think\Validate;

/**
 * 课程订单模块
 * @ApiWeigh (50)
 */
class CourseOrder extends Api
{
    protected $noNeedLogin = ["orderInfo"];
    protected $noNeedRight = ['*'];

    /**
     * 预下单
     * @ApiMethod   (POST)
     * @ApiWeigh (100)
     * @ApiParams   (name="type", type="integer",  description="类型：1：普通订单；2：拼团订单")
     * @ApiParams   (name="pay_type", type="string",  description="微信:wechat,余额:money")
     * @ApiParams   (name="data_id", type="integer",  description="课程id/拼团id")
     * @ApiParams   (name="coupon_id", type="integer",  description="优惠券id")
     *
     * @ApiReturnParams   (name="price", type="float", required=true, description="课程价格")
     * @ApiReturnParams   (name="discount_money", type="float", required=true, description="优惠金额")
     * @ApiReturnParams   (name="money", type="float", required=true, description="总金额")
     * @ApiReturnParams   (name="vip_discount", type="float", required=true, description="会员折扣")
     * @ApiReturnParams   (name="buy_course_count", type="float", required=true, description="升级会员购买课程次数")
     * @ApiReturnParams   (name="user_course_count", type="float", required=true, description="会员购买课程数量")
     * @ApiReturnParams   (name="vip_low_money", type="float", required=true, description="满足课程数会员充值金额")
     * @ApiReturnParams   (name="coupon_count", type="float", required=true, description="优惠券数量")
     */
    public function preOrder()
    {
        $type = $this->request->post("type", 1);
        $data_id = $this->request->post("data_id", 0);
        $coupon_id = $this->request->post("coupon_id", 0);
        $courseModel = new \app\admin\model\course\Course();
        $userCouponModel = new \app\admin\model\course\UserCoupon();
        $pay_type = $this->request->post("pay_type");


        try {
            //检测优惠券
            if($coupon_id > 0){
                $userCouponModel->checkUserCoupon($coupon_id,$this->auth->id);
            }
            $info = $courseModel->preOrder($type,$data_id,$coupon_id,$this->auth->id,$pay_type);

            $userinfo = $this->auth->getUserinfo();
            //购买课程次数
            $orderModel = new Order();
            $course_count = $orderModel->where("user_id",$userinfo['id'])
                ->where("status",">=", $orderModel::STATUS_PAY)
                ->count();
            $vip_discount = \config("site.vip_discount");
            $info['vip_discount'] = $vip_discount;

            $buy_course_count = \config("site.buy_course_count");
            $vip_low_money = \config("site.vip_low_money");

            $info['buy_course_count']  = (int)$buy_course_count;
            $info['user_course_count']  = $course_count;
            $info['vip_low_money']  = $vip_low_money;
            //查询商品
        }catch (\Exception $exception){
            $this->error($exception->getMessage());
        }

        $this->success("查询成功", $info);
    }


    /**
     * 创建订单
     * @ApiMethod   (POST)
     * @ApiWeigh (99)
     * @ApiParams   (name="type", type="integer",  description="类型：1：普通订单；2：拼团订单")
     * @ApiParams   (name="team_id", type="integer",  description="拼团id")
     * @ApiParams   (name="pay_type", type="string",  description="微信:wechat,余额:money")
     * @ApiParams   (name="data_id", type="integer",  description="课程id/拼团id")
     * @ApiParams   (name="coupon_id", type="integer",  description="优惠券id")
     * @ApiParams   (name="school_id", type="integer",  description="校区")
     * @ApiParams   (name="classes_id", type="integer",  description="班次")
     * @ApiParams   (name="student_id", type="integer",  description="学员id")
     * @ApiParams   (name="parent_name", type="string",  description="家长名字")
     * @ApiParams   (name="phone_1", type="string",  description="联系方式1")
     * @ApiParams   (name="phone_2", type="string",  description="联系方式2")
     * @ApiParams   (name="phone_3", type="string",  description="身份证号")
     * @ApiParams   (name="time_arr", type="object", sample="[{'time_no':'111_222','w':1},{'time_no':'111_222','w':1}]", description="时间段")
     *
     */
    public function createOrder()
    {
        $data = $this->request->post();
        if(isset($data['time_arr'])){
            unset($data['time_arr']);
        }
        $validate = new Validate([
            ["type","require|in:1,2","请选择订单类型|订单类型错误"],
            ["data_id","require","请选择信息id"],
            ["school_id","require","请选择校区"],
            ["classes_id","require","请选择班次"],
            ["student_id","require","请选择学员"],
            ["parent_name","require","请填写家长姓名"],
            ["phone_1","require","请填写联系方式1"],
            ["phone_2","require","请填写联系方式2"],
        ]);
        $result = $validate->check($data);
        if(!$result){
            $this->error($validate->getError());
        }

        $coupon_id = $this->request->post("coupon_id", 0);
        $time_arr = $this->request->post("time_arr/a",[]);
        $time_arr = array_filter($time_arr);
//        $time_arr = [
//            [
//                "w" =>5,
//                "time_no" => "18_30_20_30",
//            ],
//        ];
        $orderModel = new \app\admin\model\course\Order();
        $userCouponModel = new \app\admin\model\course\UserCoupon();
        Db::startTrans();
        try {
            //检测优惠券
            if($coupon_id > 0){
                $userCouponModel->checkUserCoupon($coupon_id,$this->auth->id);
            }
            $orderRes = $orderModel->createOrder($this->auth->getUser(), $data,$time_arr,$coupon_id);
            Db::commit();
        }catch (\Exception $exception){

            $this->error($exception->getMessage());
            Db::rollback();
        }

        $this->success("创建成功", [
            "order_id" => $orderRes['order_id'],
            "order_sn" => $orderRes['order_sn'],
            "spell_id" => $orderRes['spell_id']
        ]);
    }


    /**
     * 支付订单
     * @ApiMethod   (POST)
     * @ApiWeigh (98)
     * @ApiParams   (name="order_id", type="integer",  description="创建的订单id")
     * @ApiParams   (name="order_sn", type="integer",  description="订单号")
     * @ApiParams   (name="pay_type", type="string",  description="微信:wechat,余额:money")
     *
     */
    public function payOrder(){
        $order_id = $this->request->post("order_id");
        $order_sn = $this->request->post("order_sn");
//        $pay_from = $this->request->post("pay_from");
        $pay_from = "miniapp";
        $pay_type = $this->request->post("pay_type");
        if(!in_array($pay_from, ["miniapp", "app"])){
            $this->error("支付类型错误");
        }
        if(!in_array($pay_type, ["wechat", "alipay", "money"])){
            $this->error("支付方式错误");
        }

        $payLog = new PayLog();
        $orderModel = new Order();
        $orderGoodsModel = new OrderGoods();
        $courseModel = new \app\admin\model\course\Course();
        $orderInfo = $orderModel->where("id", $order_id)
            ->where("order_sn", $order_sn)
            ->find();
        if(!$orderInfo){
            $this->error("未找到该订单");
        }
        $orderInfo->changeOrderSn();
        $data_id = 0;
        switch ($orderInfo['type']){
            case Order::TYPE_COURSE:
                $data_id = $orderInfo['course_id'];
                break;
            case Order::TYPE_SPELL:
                $data_id = $orderInfo['spell_id'];
                break;
        }
        Db::startTrans();
        try {
            $preInfo = $courseModel->preOrder($orderInfo['type'],$data_id,$orderInfo['coupon_id'],$this->auth->id,$pay_type,true);
            $orderInfo->save([
                "money" => $preInfo['money'],
                "course_price" => $preInfo['price'],
                "discount_money" => $preInfo['discount_money'],
                "vip_discount_money" => $preInfo['vip_discount_money'],
                "pay_type" => $pay_type
            ]);
            $aliPay = "";
            $wechatPay = [];

            //检测订单
            $course = $courseModel->getInfo(["id"=>$orderInfo['course_id']]);
            $time_arr = collection($orderGoodsModel->where("order_id",$orderInfo['id'])->field("w,time_no")->select())->toArray();
//            dump($time_arr);exit;
            $orderModel->checkOrder($time_arr,$course,$orderInfo,$this->auth->getUser());
//            dump($a)
            switch ($pay_type){
                case "money":
                    //余额支付
                    $orderInfo->payOrder($this->auth->getUser(), $pay_from, $pay_type);
                    break;
                case "wechat":
                    //微信支付
                    $params = [
                        'type'         => $pay_type,
                        'orderid' => $orderInfo["order_sn"],
                        'title'        => "购买商品",
                        'amount'       => $orderInfo["money"],
                        'method'       => $pay_from,
                        'notifyurl'    => $this->request->domain() . "/api/common/notify/type/" . $pay_type . "/from/" . $pay_from,
                        'returnurl'    => $this->request->domain(),
                    ];
                    if($pay_from == "miniapp"){
                        $openid = Third::where("user_id",$this->auth->id)->where("platform","wechatMiniApp")->value("openid");
                        if(!$openid){
                            exception("当前小程序用户没有小程序id,请联系管理员");
                        }
                        $params["openid"] = $openid;
                    }

                    $wechatPay =  Service::submitOrder($params);
                    $payLog->createPayLog($this->auth->getUser(), $orderInfo["order_sn"], $orderInfo["money"], $pay_from, $pay_type);
                    break;
                case "alipay":
                    //支付宝支付
                    $params = [
                        'type'         => $pay_type,
                        'orderid' => $orderInfo["order_sn"],
                        'title'        => "购买商品",
                        'amount'       => $orderInfo["money"],
                        'method'       => $pay_from,
                        'notifyurl'    => $this->request->domain() . "/api/common/notify/type/" . $pay_type. "/from/" . $pay_from,
                        'returnurl'    => $this->request->domain(),
                    ];
                    $aliPay =  Service::submitOrder($params);
                    $payLog->createPayLog($this->auth->getUser(), $orderInfo["order_sn"], $orderInfo["money"], $pay_from, $pay_type);
                    break;
            }

            $res = [
                "wechatPay" => $wechatPay,
                "aliPay" => $aliPay,
            ];
            Db::commit();
        }catch (\Exception $exception){
            $this->error($exception->getMessage());
            Db::rollback();
        }


        $condition = [
//            "user_id" => $this->auth->id,
            "order.id" => $order_id,
        ];
        $orderInfo = $orderModel->getOrderInfo($condition);
        $res["orderInfo"] = $orderInfo;
        $this->success("支付成功", $res);
    }


    /**
     * 订单列表
     * @ApiMethod   (POST)
     * @ApiWeigh (97)
     * @ApiParams   (name="page", type="integer",  description="页码")
     * @ApiParams   (name="status", type="integer",  description="状态-2:停课；-1：已取消；10待支付；20：已支付；21：未排课；100：已完成；-100：已退款")
     *
     * @ApiReturnParams   (name="order_sn", type="string", required=true, description="订单号")
     * @ApiReturnParams   (name="money", type="string", required=true, description="订单金额")
     * @ApiReturnParams   (name="course_price", type="string", required=true, description="价格")
     * @ApiReturnParams   (name="discount_money", type="string", required=true, description="优惠金额")
     * @ApiReturnParams   (name="pay_time", type="string", required=true, description="支付时间")
     * @ApiReturnParams   (name="complete_time", type="string", required=true, description="完成时间")
     * @ApiReturnParams   (name="status", type="string", required=true, description="-1：已取消；10待支付；20：已支付；100：已完成；-100：已退款")
     * @ApiReturnParams   (name="status_text", type="string", required=true, description="状态文字")
     */
    public function orderList(){
        $status = $this->request->post("status");
        $order = new Order();
        $condition = [
            "order.user_id" => $this->auth->id,
        ];
        if($status){
            if($status == $order::STATUS_UN_TEACHER){
                $ids = OrderGoods::where("teacher_id",0)->where("user_id",$this->auth->id)->column("order_id");
                $condition['order.id'] = ["in", $ids];
                $condition["order.status"] = $order::STATUS_PAY;
            }elseif ($status == $order::STATUS_PAY){
                $ids = OrderGoods::where("teacher_id",0)->where("user_id",$this->auth->id)->column("order_id");
                $condition['order.id'] = ["notin", $ids];
                $condition["order.status"] = $status;
            }else{
                $condition["order.status"] = $status;
            }

        }

        $list = $order->getOrderList($condition);
        $this->success("查询成功", $list);
    }

    /**
     * 订单详情
     * @ApiMethod   (POST)
     * @ApiWeigh (96)
     * @ApiParams   (name="id", type="integer",  description="订单id")
     *
     * @ApiReturnParams   (name="order_sn", type="string", required=true, description="订单号")
     * @ApiReturnParams   (name="money", type="string", required=true, description="订单金额")
     * @ApiReturnParams   (name="goods_price", type="string", required=true, description="商品价格")
     * @ApiReturnParams   (name="freight_money", type="string", required=true, description="运费")
     * @ApiReturnParams   (name="discount_money", type="string", required=true, description="优惠金额")
     * @ApiReturnParams   (name="pay_time", type="string", required=true, description="支付时间")
     * @ApiReturnParams   (name="deliver_time", type="string", required=true, description="发货时间")
     * @ApiReturnParams   (name="complete_time", type="string", required=true, description="完成时间")
     * @ApiReturnParams   (name="express_company", type="string", required=true, description="快递公司")
     * @ApiReturnParams   (name="express_no", type="string", required=true, description="快递单号")
     * @ApiReturnParams   (name="address_info", type="string", required=true, description="地址")
     * @ApiReturnParams   (name="address_mobile", type="string", required=true, description="地址电话")
     * @ApiReturnParams   (name="status", type="string", required=true, description="-10：退换货；-1：已取消；10待支付；20：待发货；30：待收货；40：待评价；50：已完成；-100：已退款")
     * @ApiReturnParams   (name="status_text", type="string", required=true, description="状态文字")
     * @ApiReturnParams   (name="return_status_text", type="string", required=true, description="退换货状态")
     * @ApiReturnParams   (name="return_type_text", type="string", required=true, description="退换货类型")
     * @ApiReturnParams   (name="pay_from_text", type="string", required=true, description="支付平台")
     * @ApiReturnParams   (name="pay_type_text", type="string", required=true, description="支付方式")
     * @ApiReturnParams   (name="is_other_pay", type="string", required=true, description="是否是代付订单")
     * @ApiReturnParams   (name="pay_user", type="string", required=true, description="代付人")
     * @ApiReturnParams   (name="cancel_time_text", type="string", required=true, description="取消时间")
     */
    public function orderInfo(){
        $order_id = $this->request->post("id");
        $order = new Order();
        $condition = [
//            "order.user_id" => $this->auth->id,
            "order.id" => $order_id,
        ];

        try {
            $res = $order->getOrderInfo($condition);
        }catch (\Exception $exception){
            $this->error($exception->getMessage());
        }
        $this->success("查询成功", $res);
    }



    /**
     * 往期课程
     * @ApiMethod   (POST)
     * @ApiWeigh (94)
     *
     * @ApiParams   (name="type", type="string", required=true, description="1:所有;2:上课中的；3:已完成的")
     * @ApiReturnParams   (name="order_sn", type="string", required=true, description="订单号")
     * @ApiReturnParams   (name="money", type="string", required=true, description="订单金额")
     * @ApiReturnParams   (name="course_price", type="string", required=true, description="价格")
     * @ApiReturnParams   (name="discount_money", type="string", required=true, description="优惠金额")
     * @ApiReturnParams   (name="pay_time", type="string", required=true, description="支付时间")
     * @ApiReturnParams   (name="complete_time", type="string", required=true, description="完成时间")
     * @ApiReturnParams   (name="status", type="string", required=true, description="-1：已取消；10待支付；20：已支付；100：已完成；-100：已退款")
     * @ApiReturnParams   (name="status_text", type="string", required=true, description="状态文字")
     */
    public function studentOrderList(){
        $type = $this->request->post("type");
        $order = new Order();
        $studentCondition = [];
        switch ($type){
            case 1: //所有
                $studentCondition = [
                    "user_id" => $this->auth->id,
                    "status" => ['in', [$order::STATUS_STOP,$order::STATUS_PAY,$order::STATUS_UN_TEACHER,$order::STATUS_TEACHER,$order::STATUS_SUCCESS]],
                ];
                break;
            case 2: //上课中的
                $studentCondition = [
                    "user_id" => $this->auth->id,
                    "status" => $order::STATUS_PAY,
                ];
                break;
            case 3: //已完成的
                $studentCondition = [
                    "user_id" => $this->auth->id,
                    "status" => $order::STATUS_SUCCESS,
                ];
                break;
        }

        $studentIds = $order->where($studentCondition)->column("student_id");
//        dump($this->auth->id);exit;
        $studentModel = new Student();
        $list = $studentModel->where("id","in",$studentIds)->select();
        foreach ($list as $key => $value){
            switch ($type){
                case 1: //所有
                    $condition = [
                        "order.status" => ['in', [$order::STATUS_STOP,$order::STATUS_PAY,$order::STATUS_UN_TEACHER,$order::STATUS_TEACHER,$order::STATUS_SUCCESS,$order::STATUS_CANCEL]],
                        "order.student_id" => $value['id'],
                    ];
                    break;
                case 2: //上课中的
                    $condition = [
                        "order.status" => $order::STATUS_PAY,
                        "order.student_id" => $value['id'],
                    ];
                    break;
                case 3: //已完成的
                    $condition = [
                        "order.status" => $order::STATUS_SUCCESS,
                        "order.student_id" => $value['id'],
                    ];
                    break;
            }


            $value['order_list'] = $order->getOrderList($condition,0);
        }
        $this->success("查询成功",$list);

    }


    /**
     * 签到日历
     *
     * @ApiMethod (POST)
     * @ApiWeigh (93)
     * @ApiParams   (name="order_id", type="string", required=true, description="订单id")
     * @ApiParams   (name="day", type="string", required=true, description="日期2023-01-01")
     */
    public function calendar()
    {
        $day = $this->request->post('day');
        if($day){
            $starttime = strtotime(date('Y-m',strtotime($day)));
            $endtime = strtotime(date('Y-m',strtotime('+1 month',strtotime( date("Y-m", $starttime)))));
        } else {
            $starttime = Date::unixtime('month', 0, 'start');
            $endtime = Date::unixtime('month', 0, 'end');
        }

        $order_id = $this->request->post('order_id');
        $orderGoodsModel = new OrderGoods();
        try {
            $orderInfo = Order::get($order_id);
            if(!$orderInfo){
                exception("未找到当前订单");
            }
            //日历
            $orderGoodsCondition = [
                "order_goods.order_id" => $order_id,
                "order_goods.student_id" => $orderInfo['student_id'],
            ];
            $orderGoods = $orderGoodsModel->getTimeTable($orderGoodsCondition,"",false);
            $orderSignModel = new OrderSign();
            //签到日志
            $signLogcondition = [
                "order_sign.student_id" => $orderInfo['student_id'],
                "order_sign.course_id" => $orderGoods['course_id'],
                "order_sign.school_id" => $orderGoods['school_id'],
                "order_sign.classes_id" => $orderGoods['classes_id'],
//                "order_sign.time_no" => $orderGoods['time_no'],
                "order_sign.teacher_id" => $orderGoods['teacher_id'],
                "order_sign.w" => $orderGoods['w'],
                "order_sign.day" => ["between",[date("Y-m-d", $starttime),date("Y-m-d", $endtime)]],
//                "status" => $orderSignModel::STATUS_SIGN
            ];
            $signLog = $orderSignModel->getSignLog($signLogcondition,2);


        }catch (\Exception $exception){
            $this->error($exception->getMessage());
        }
        $this->success("查询成功",$signLog);
    }



    /**
     * 成长
     *
     * @ApiMethod (POST)
     * @ApiWeigh (92)
     * @ApiParams   (name="order_id", type="string", required=true, description="订单id")
     * @ApiParams   (name="day", type="string", required=true, description="日期2023-01-01")
     */
    public function growUp()
    {
        $day = $this->request->post('day');
        $order_id = $this->request->post('order_id');
        $orderGoodsModel = new OrderGoods();

        try {
            //课程详情
            $orderGoodsCondition = [
                "order_goods.order_id" => $order_id,
            ];
            $orderGoodsList = $orderGoodsModel->getTimeTable($orderGoodsCondition,$day,true);
            //学习记录

            $orderGoods = $orderGoodsModel->getTimeTable($orderGoodsCondition,$day,false);
            $orderSignModel = new OrderSign();
            //签到日志
            $signLogcondition = [
                "order_sign.course_id" => $orderGoods['course_id'],
                "order_sign.school_id" => $orderGoods['school_id'],
                "order_sign.classes_id" => $orderGoods['classes_id'],
//                "order_sign.time_no" => $orderGoods['time_no'],
                "order_sign.teacher_id" => $orderGoods['teacher_id'],
                "order_sign.w" => $orderGoods['w'],
                "order_sign.day" => $day,
//                "order_sign.status" => $orderSignModel::STATUS_SIGN
            ];
            $signLog = $orderSignModel->getSignLog($signLogcondition,2);
            $res = [
                "orderGoods" => $orderGoodsList,
                "signLog" => $signLog,
            ];
        }catch (\Exception $exception){
            $this->error($exception->getMessage());
        }
        $this->success("查询成功",$res);
    }

    /**
     * 学习记录
     *
     * @ApiMethod (POST)
     * @ApiWeigh (91)
     * @ApiParams   (name="order_id", type="string", required=true, description="order_id")
     * @ApiParams   (name="start_day", type="string", required=true, description="日期2023-01-01")
     * @ApiParams   (name="end_day", type="string", required=true, description="日期2023-01-01")
     */
    public function signLogList()
    {
        $start_day = $this->request->post('start_day');
        $end_day = $this->request->post('end_day');
        $order_id = $this->request->post('order_id');
        $orderGoodsModel = new OrderGoods();

        try {
            //课程详情
            $orderGoodsCondition = [
                "order_goods.order_id" => $order_id,
            ];
            //学习记录
            $orderGoods = $orderGoodsModel->getTimeTable($orderGoodsCondition,"",false,[]);

//            dump($orderGoods);exit;
            $orderSignModel = new OrderSign();
            //签到日志
            $signLogcondition = [
                "order_sign.course_id" => $orderGoods['course_id'],
                "order_sign.school_id" => $orderGoods['school_id'],
                "order_sign.classes_id" => $orderGoods['classes_id'],
//                "order_sign.time_no" => $orderGoods['time_no'],
                "order_sign.teacher_id" => $orderGoods['teacher_id'],
                "order_sign.student_id" => $orderGoods['student_id'],
//                "order_sign.w" => $orderGoods['w'],
                "order_sign.is_jilu" => 1,
//                "order_sign.status" => $orderSignModel::STATUS_SIGN
            ];
            if($start_day && $end_day){
                $signLogcondition['order_sign.day'] = ["between",[$start_day,$end_day]];
            }
            $signLog = $orderSignModel->getSignLog($signLogcondition,2);

        }catch (\Exception $exception){
            $this->error($exception->getMessage());
        }
        $this->success("查询成功",$signLog);
    }

    /**
     * 学校记录详情
     *
     * @ApiMethod (POST)
     * @ApiWeigh (93)
     * @ApiParams   (name="id", type="string", required=true, description="学校记录id")
     */
    public function signInfo()
    {
        $id = $this->request->post('id');
        $orderSignModel = new OrderSign();
        try {
            $signLog = $orderSignModel->get($id);
        }catch (\Exception $exception){
            $this->error($exception->getMessage());
        }
        $this->success("查询成功",$signLog);
    }

    /**
     * 课程表（请假）
     *
     * @ApiMethod (POST)
     * @ApiWeigh (80)
     * @ApiParams   (name="student_id", type="string", required=true, description="学员id")
     * @ApiParams   (name="day", type="string", required=true, description="日期2023-01-01")
     */
    public function courseList()
    {
        $day = $this->request->post('day');
        $student_id = $this->request->post('student_id');
        $condition = [
            "order_goods.student_id" => $student_id,
        ];
        $orderGoodsModel = new OrderGoods();
        $list = $orderGoodsModel->getTimeTable($condition,$day);

        $this->success("",$list);
    }


    /**
     * 请假
     * @ApiMethod   (POST)
     * @ApiWeigh (79)
     * @ApiParams   (name="student_id", type="integer",  description="学员id")
     * @ApiParams   (name="day", type="integer",  description="日期")
     * @ApiParams   (name="order_goods_id", type="integer",  description="课表id")
     * @ApiParams   (name="leave_content", type="integer",  description="请假理由")
     *
     */
    public function askLeave(){
        $day = $this->request->post('day');
        $student_id = $this->request->post('student_id');
        $order_goods_id = $this->request->post('order_goods_id');
        $leave_content = $this->request->post('leave_content');
        $condition = [
            "order_goods.student_id" => $student_id,
            "order_goods.id" => $order_goods_id,
        ];
        $orderGoodsModel = new OrderGoods();
        $orderSignModel = new OrderSign();
        Db::startTrans();
        try {
            $orderGoods = $orderGoodsModel->getTimeTable($condition,$day,false);
            $orderSignModel->sign($orderGoods,$day,$orderSignModel::STATUS_LEAVE,$leave_content);
            Db::commit();
        }catch (\Exception $e){
            Db::rollback();
            $this->error($e->getMessage());
        }
        $this->success("请假成功");
    }

    /**
     * 请假列表
     * @ApiMethod   (POST)
     * @ApiWeigh (78)
     *
     * @ApiReturnParams   (name="createtime_text", type="string",  description="请假申请时间")
     * @ApiReturnParams   (name="day", type="string",  description="请假日期")
     * @ApiReturnParams   (name="time_name", type="string",  description="请假时间段")
     */
    public function askLeaveList(){
        $orderSignModel = new OrderSign();
        Db::startTrans();
        try {
            $condition = [
                "order_sign.user_id" => $this->auth->id,
                "order_sign.status" => $orderSignModel::STATUS_LEAVE,
            ];
            $list = $orderSignModel->askSignList($condition);
            Db::commit();
        }catch (\Exception $e){
            Db::rollback();
            $this->error($e->getMessage());
        }
        $this->success("查询成功",$list);
    }


    /**
     * 申请转校
     * @ApiMethod   (POST)
     * @ApiWeigh (70)
     *
     * @ApiParams   (name="student_id", type="string",  description="学员id")
     * @ApiParams   (name="order_id", type="string",  description="订单id")
     * @ApiParams   (name="type", type="string",  description="类型：1：转老师；2：转学校")
     * @ApiParams   (name="reason", type="string",  description="理由")
     */
    public function applyTransfer(){
        $data = $this->request->post();

        $validate = new Validate([
            ["student_id","require","请选择学员"],
            ["order_id","require","请选择订单"],
            ["type","require|in:1,2","请选择类型|类型错误"],
            ["reason","require","请填写理由"],
        ]);
        $result = $validate->check($data);
        if(!$result){
            $this->error($validate->getError());
        }


        $transferModel = new Transfer();
        $order = new Order();
        Db::startTrans();
        try {
            $condition = [
                "order.user_id" => $this->auth->id,
                "order.id" => $data['order_id'],
                "order.student_id" => $data['student_id'],
                "order.status" => $order::STATUS_PAY,
            ];
            $orderInfo = $order->getOrderInfo($condition);
            if($orderInfo['surplus_class_hour'] <=0){
                exception("当前课程已经没有剩余课时了");
            }
            $data['user_id'] =$this->auth->id;
            $data['course_id'] =$orderInfo['course']['id']??'';
            $data['course_name'] =$orderInfo['course']['name']??'';
            $data['school_id'] =$orderInfo['school_id']??0;
            $data['school_name'] =$orderInfo['school_name']??'';
//            $class_hour = $orderInfo['class_hour']??0;
//            $surplus_class_hour = $orderInfo['surplus_class_hour']??0;
//
//            $data['old_class_hour'] =$class_hour;
//            $data['old_use_class_hour'] = $class_hour - $surplus_class_hour;
//            $data['old_surplus_class_hour'] = $surplus_class_hour;
//            $data['old_surplus_price'] = $orderInfo['surplus_price'];
            $transferModel->applyTransfer($data);
            Db::commit();
        }catch (\Exception $e){
            Db::rollback();
            $this->error($e->getMessage());
        }
        $this->success("申请成功");
    }


    /**
     * 申请列表
     * @ApiMethod   (POST)
     * @ApiWeigh (69)
     *
     * @ApiParams   (name="type", type="string",  description="类型：1：转老师；2：转学校")
     * @ApiParams   (name="status", type="string",  description="状态：-1：拒绝；1：处理中；2：已完成")
     * @ApiParams   (name="page", type="string",  description="页码")
     * @ApiReturnParams   (name="reason", type="string",  description="申请理由")
     * @ApiReturnParams   (name="type", type="string",  description="类型：1：转老师；2：转学校")
     * @ApiReturnParams   (name="status", type="string",  description="状态：-1：拒绝；1：处理中；2：已完成")
     * @ApiReturnParams   (name="refuse_content", type="string",  description="拒绝原因")
     */
    public function transferList(){

        $type = $this->request->post("type");
        $status = $this->request->post("status");

        $transferModel = new Transfer();
        $condition = [
            "transfer.user_id" => $this->auth->id
        ];
        if($type){
            $condition['type'] = $type;
        }
        if($status){
            $condition['status'] = $status;
        }
        try {
            $list = $transferModel->transferList($condition);
        }catch (\Exception $e){
            $this->error($e->getMessage());
        }
        $this->success("查询成功",$list);
    }

    /**
     * 申请转校详情
     * @ApiMethod   (POST)
     * @ApiWeigh (68)
     *
     * @ApiParams   (name="id", type="string",  description="id")
     * @ApiReturnParams   (name="reason", type="string",  description="申请理由")
     * @ApiReturnParams   (name="type", type="string",  description="类型：1：转老师；2：转学校")
     * @ApiReturnParams   (name="status", type="string",  description="状态：-1：拒绝；1：处理中；2：已完成")
     * @ApiReturnParams   (name="refuse_content", type="string",  description="拒绝原因")
     */
    public function transferInfo(){

        $id = $this->request->post("id");
        $transferModel = new Transfer();
        try {
            $condition = [
                "transfer.id" => $id
            ];
            $info = $transferModel->transferInfo($condition);
        }catch (\Exception $e){

            $this->error($e->getMessage());
        }
        $this->success("查询成功",$info);
    }

}
